package io.gitee.minelx.filesystem.values;

public final class URI {

	private final SchemeAndAuthority schemeAndAuthority;

	private final String path;

	private URI(SchemeAndAuthority schemeAndAuthority, String path) {
		this.schemeAndAuthority = schemeAndAuthority;
		this.path = path;
	}

	public SchemeAndAuthority schemeAndAuthority() {
		return schemeAndAuthority;
	}

	public String scheme() {
		return schemeAndAuthority.scheme();
	}

	public String authority() {
		return schemeAndAuthority.authority();
	}

	public String path() {
		return path;
	}

	public static URI of(String scheme, String authority, String path) {
		return new URI(SchemeAndAuthority.of(scheme, authority), path);
	}

	public static URI from(String uriAsText) {
		java.net.URI javaURI = java.net.URI.create(uriAsText);
		return URI.of(
				javaURI.getScheme(),
				authorityOfJavaURI(javaURI),
				javaURI.getPath());
	}

	private static String authorityOfJavaURI(java.net.URI javaURI) {
		if (javaURI.getAuthority() == null) {
			return "";
		}
		return javaURI.getAuthority();
	}
}
